package Judge;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.R;

import java.util.ArrayList;
import java.util.List;

import dateBase.JudgeAdapter;
import dateBase.Judge_Site_DBHelper;
import util.ToastUtil;

public class Judge_del extends AppCompatActivity implements View.OnClickListener {
    private Button btnSelect, btndel;//删除
    private List<JudgeBean> list = new ArrayList<JudgeBean>();
    private EditText editKey;
    private ListView listView;
    private String name;
    private Judge_Site_DBHelper mHelper;
    private JudgeBean judge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_judge_del);
        btndel = (Button) findViewById(R.id.btndel);
        listView = (ListView) findViewById(R.id.lv_JS);
        initView();


    }

    @Override
    protected void onStart() {
        super.onStart();
        mHelper = Judge_Site_DBHelper.getInstance(this);
        //打开读写链接
        mHelper.openReadLink();
        mHelper.openWriteLink();
        //获得帮助器实例
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHelper.closeLink();
    }

    public void initView() {
        editKey = (EditText) findViewById(R.id.editkey);
        findViewById(R.id.btndel).setOnClickListener(this);
        findViewById(R.id.btnselect).setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == -1)
                    return;

                // 获取点击的行数
                int clickedPosition = position;
                // 获取该行对应的数据对象
                if (listView.getAdapter() == null || listView.getAdapter().getCount() == 0) {
                    return;
                } else {
                    Object clickedObject = listView.getItemAtPosition(clickedPosition);
                    // 将对象强制转换为你期望的类型
                    JudgeBean clickedData = (JudgeBean) clickedObject;
                    name = clickedData.getJname();
                    ToastUtil.show(Judge_del.this, "已选中");
                }

            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btndel:

                if (mHelper.deleteByName(name) > 0) {
                    list = mHelper.queryByName(name);
                    {
                        JudgeAdapter adapter = new JudgeAdapter(Judge_del.this, list);
                        listView.setAdapter(adapter);

                    }
                    ToastUtil.show(this, "删除成功");

                } else {
                    ToastUtil.show(this, "删除失败");
                    break;
                }
                break;
            case R.id.btnselect:
                name = editKey.getText().toString();
                ToastUtil.show(Judge_del.this, name);
                list = mHelper.queryByName(name);
                listView = findViewById(R.id.lv_JS);
                if (list.isEmpty()) {
                    listView.setAdapter(null);
                    ToastUtil.show(Judge_del.this, "检索信息不存在");
                } else {
                    JudgeAdapter adapter = new JudgeAdapter(Judge_del.this, list);
                    listView.setAdapter(adapter);
                    ToastUtil.show(Judge_del.this, "查询成功");
                }
                break;
        }
    }

}
